﻿using Microsoft.Practices.Unity;
using Sasa.QualityTools.CodeAnalysis.Core.VisualStudio;
using System.Collections.Generic;
using System.Linq;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Commands.Behaviors.CodeAnalysis.Inspection
{
    public class CreateItemInspectorBehavior : CreateCodeInspectorBehaviorBase
    {
        public CreateItemInspectorBehavior(IUnityContainer container)
            : base(container)
        {
        }


        public override void Behave(CodeAnalysisCommandExecutionContext context)
        {
            var ide = Container.Resolve<IIde>();

            var typeNames = new List<string>();
            foreach (var item in ide.SelectedProjectItems)
            {
                if (item.CodeModel.IsSupportedLanguage)
                {
                    typeNames.AddRange(item.CodeModel.TypeNames);
                }
            }

            if (typeNames.Count == 0)
            {
                context.Cancel("No analyzable type is found.");
                return;
            }

            var project = ide.SelectedProjectItems.ElementAt(0).ContainingProject;
            context.Progress.CodeAnalyzers.Add(BuildupCodeAnalysis(project, typeNames.ToArray()));
        }
    }
}
